package com.btr.ivle.helper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.btr.ivle.R;

public class ForumSeparatedListAdapter extends ArrayAdapter<Map<String,String>> {
	
	Context context;
	
    public ForumSeparatedListAdapter(Context context, List<Map<String, String>> list) {
        super(context, 0, list);
        this.context = context;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy hh:mm a");
        Date date;
    	View view = convertView;
    	
        if(getItem(position).containsKey("Title")){
            view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item_tag, null);
            TextView tvTitle = (TextView) view.findViewById(R.id.group_list_item_text);
            tvTitle.setText(getItem(position).get("Title"));
        }else if(getItem(position).containsKey("topLevel")){
        	calendar.setTimeInMillis(Long.parseLong(getItem(position).get("PostDate")));
            date = calendar.getTime();
            
            view = LayoutInflater.from(getContext()).inflate(R.layout.group_list_item_forum, null);
            TextView tvTitle = (TextView) view.findViewById(R.id.group_list_item_text);
            tvTitle.setText(getItem(position).get("PostTitle"));
            TextView tvPostBody = (TextView) view.findViewById(R.id.group_list_item_body);
            String htmltext = getItem(position).get("PostBody").replace("<br />", " ");
            tvPostBody.setText(Html.fromHtml(htmltext));
            TextView tvTime = (TextView) view.findViewById(R.id.group_list_item_time);
            tvTime.setText(dateFormat.format(date));
            TextView tvPoster = (TextView) view.findViewById(R.id.group_list_item_poster);
            tvPoster.setText(getItem(position).get("PosterName"));
            if(getItem(position).containsKey("isPosterStaff")){
            	tvPoster.setTextColor(context.getResources().getColor(R.color.dark_green));
        	}
            if(getItem(position).containsKey("isNewPost")){
            	ImageView im = (ImageView) view.findViewById(R.id.icon);
            	im.setImageResource(R.drawable.hasnewpost);
            }
        }
        return view;
    }
    
    @Override
    public boolean isEnabled(int position) {
        if(getItem(position).containsKey("Title")){
            return false;
        }
        return super.isEnabled(position);
    }
}
